library(dplyr)
library(ggplot2)
library(gapminder)
library(plotly)

knitr::opts_chunk$set(fig.width = 10, fig.height = 5, warning = FALSE, message = FALSE)

Verificando os tipos de colunas do dataset

glimpse(gapminder)
## Observations: 1,704
## Variables: 6
## $ country   <fct> Afghanistan, Afghanistan, Afghanistan, Afghanistan, ...
## $ continent <fct> Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia...
## $ year      <int> 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992...
## $ lifeExp   <dbl> 28.801, 30.332, 31.997, 34.020, 36.088, 38.438, 39.8...
## $ pop       <int> 8425333, 9240934, 10267083, 11537966, 13079460, 1488...
## $ gdpPercap <dbl> 779.4453, 820.8530, 853.1007, 836.1971, 739.9811, 78...

Filtrando por year == 2007

gap07 = gapminder %>%
  filter(year == 2007)

Utilizando as opções default

gap07 %>%
  ggplot(aes(x = gdpPercap, y = lifeExp)) +
  geom_point()

Alterando o tema

gap07 %>%
  ggplot(aes(x = gdpPercap, y = lifeExp)) +
  geom_point() +
  theme_classic()

gap07 %>%
  ggplot(aes(x = gdpPercap, y = lifeExp)) +
  geom_point() +
  theme_bw()

Aumentando o tamanho dos pontos

gap07 %>%
  ggplot(aes(x = gdpPercap, y = lifeExp)) +
  geom_point(size = 2) +
  theme_bw()

Inserindo informação do continente

gap07 %>%
  ggplot(aes(x = gdpPercap, y = lifeExp, col = continent)) +
  geom_point(size = 2) +
  theme_bw()

gap07 %>%
  ggplot(aes(x = gdpPercap, y = lifeExp, col = continent, shape = continent)) +
  geom_point(size = 2) +
  theme_bw()

Inserindo informação da população no tamanho do ponto

gap07 %>%
  ggplot(aes(x = gdpPercap, y = lifeExp, col = continent)) +
  geom_point(aes(size = pop)) +
  theme_bw()

Alterando os labels

gap07 %>%
  ggplot(aes(x = gdpPercap, y = lifeExp, col = continent)) +
  geom_point(aes(size = pop)) +
  theme_bw() +
  labs(x = "GDP per capita", y = "Life Expectancy", col = "Continent", size = "Population")

Reordenando as legendas

gap07 %>%
  ggplot(aes(x = gdpPercap, y = lifeExp, col = continent)) +
  geom_point(aes(size = pop)) +
  theme_bw() +
  labs(x = "GDP per capita", y = "Life Expectancy", col = "Continent", size = "Population") +
  guides(color = guide_legend(order = 1),
         size = guide_legend(order = 2))

Removendo uma legenda

gap07 %>%
  ggplot(aes(x = gdpPercap, y = lifeExp, col = continent)) +
  geom_point(aes(size = pop)) +
  theme_bw() +
  labs(x = "GDP per capita", y = "Life Expectancy", col = "Continent", size = "Population") +
  guides(color = guide_legend(order = 1),
         size = FALSE)

Inserindo tooltips e zoom com a função plotly::ggplotly()

p = gap07 %>%
  ggplot(aes(x = gdpPercap, y = lifeExp, col = continent)) +
  geom_point(aes(size = pop)) +
  theme_bw() +
  labs(x = "GDP per capita", y = "Life Expectancy", col = "Continent", size = "Population") +
  guides(color = guide_legend(order = 1),
         size = FALSE)

ggplotly(p)

Inserindo o país na tooltip

p = gap07 %>%
  mutate(text = paste("country:", country)) %>%
  ggplot(aes(x = gdpPercap, y = lifeExp, col = continent, text = text)) +
  geom_point(aes(size = pop)) +
  theme_bw() +
  labs(x = "GDP per capita", y = "Life Expectancy", col = "Continent", size = "Population") +
  guides(color = guide_legend(order = 1),
         size = FALSE)

ggplotly(p)